package cn.wolfcode.crm.service.imp;

import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.mapper.ProductMapper;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.ProductQueryObject;
import cn.wolfcode.crm.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;


    public Product selectByPrimaryKey(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    public List<Product> selectAll() {
        return productMapper.selectAll();
    }

    @Override
    public int selectByCount() {
        int count = productMapper.selectByCount();
        return count;
    }

    @Override
    public BigDecimal selectByAvg() {
        return productMapper.selectByAvg();
    }

    @Override
    public List<Product> selectExportData(ProductQueryObject qo) {
        return productMapper.selectForExport(qo);
    }

    //页面信息对象,根据easyUI的DataGrid组件,返回的数据为json对象,且只有total,rows两个属性
    public PageResult query(ProductQueryObject qo) {
        int count = productMapper.selectCount(qo);
        if (count==0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Product> rows = productMapper.queryForList(qo);
        return new PageResult(count,rows);
    }


    public void saveOrUpdate(Product product) {
        if (product.getId() == null) {
            productMapper.insert(product);
        }else{
            productMapper.updateByPrimaryKey(product);
        }
    }

    public void delete(Long id) {
        productMapper.deleteByPrimaryKey(id);
    }


}
